package com.easyfun.healthmagicbox.comm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.PersonProfilesListJsonWrapper;
import com.easyfun.healthmagicbox.comm.json.TopHealthJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.TopHealth;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;

public class TopHealthRequestTask extends BaseCommTask implements Runnable
	,DatabaseAccess{
	String personID;
	String valueData;
	String valueType;

	public TopHealthRequestTask(String personID, Context context,
			CommCallback callback,String valueData1,String valueType1) 
	{

		super(context,callback);
		this.valueData = valueData1;
		this.valueType = valueType1;
		
		this.personID = personID;
	}
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}
	
	@Override
	public void run() {
		JSONObject serializedObj = new JSONObject();
		String result = "";
		try {
			serializedObj.put("personID", personID);
			serializedObj.put("valueType", valueType);
			serializedObj.put("valueData", valueData);
			

			Map<String, String> parmas = new HashMap<String, String>();
			parmas.put("jsonstring", serializedObj.toString());
			result = super.uploadByHTTP(parmas, ConstantData.TOPRequestURI);
			if (ServerResponseCode.parseOf(result) == ServerResponseCode.FAILED) {

				ccb.setCommResult(this, result,
						ServerResponseCode.FAILED);
				return;
			}

			if ((result.length() > 0)) {
				if (ServerResponseCode.parseOf(result) == ServerResponseCode.SUCCESS) {
					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				} else {
					Dao<TopHealth, Integer> ppdao = getHelper(mcontext)
							.getTopDao();
					TopHealth data =  
							TopHealthJsonWrapper
							.getInstance(result,TopHealthJsonWrapper.class).getMpp();

					
					
						ppdao.create(data);
					
					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ccb.setCommResult(this, result,
					ServerResponseCode.FAILED);
		}
	}
}
